﻿//****************************************************************************************************
//Author:          Jun Wang
//Guid:		       f155dfbb-dcd3-429d-b7e5-4a28102be9aa
//DateTime:        3/4/2013 2:17:50 PM
//Email Address:   edu-wangjun@gedu.org
//FileName:        EventsHelper
//CLR Version:     4.0.30319.18033
//Machine Name:    META-DESKTOP
//Namespace:       RuanRang.Media.Core.Common
//Function:                
//Description:    
//****************************************************************************************************
using System;

namespace RuanRang.Media.Core.Common
{
    internal static class EventsHelper
    {
        /// <summary>
        /// Execute Raise Event
        /// </summary>
        /// <param name="singleInvoke">The single invoke.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="arg">The arg.</param>
        public delegate void ExecuteRaiseEvent(Delegate singleInvoke, object sender, object arg);

        /// <summary>
        /// Gets or sets the execute raise event delegate.
        /// </summary>
        /// <value>
        /// The execute raise event delegate.
        /// </value>
        public static ExecuteRaiseEvent ExecuteRaiseEventDelegate { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance can raise event.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance can raise event; otherwise, <c>false</c>.
        /// </value>
        public static bool CanRaiseEvent { get; set; }

        /// <summary>
        /// Initializes the <see cref="EventsHelper" /> class.
        /// </summary>
        static EventsHelper()
        {
            CanRaiseEvent = false;
        }

        /// <summary>
        /// Raises the event.
        /// </summary>
        /// <typeparam name="TSender">The type of the sender.</typeparam>
        /// <typeparam name="THandler">The type of the handler.</typeparam>
        /// <param name="handler">The handler.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="arg">The arg.</param>
        public static void RaiseEvent<TSender, THandler>(VlcEventHandler<TSender, THandler> handler, TSender sender, VlcEventArgs<THandler> arg)
        {
            if (handler == null || ExecuteRaiseEventDelegate == null)
                return;
            foreach (VlcEventHandler<TSender, THandler> singleInvoke in handler.GetInvocationList())
            {
                if (!CanRaiseEvent)
                    return;
                ExecuteRaiseEventDelegate(singleInvoke, sender, arg);
            }
        }
    }
}
